package com.css.project.process.modeler.service.impl;

import com.css.common.utils.StringUtils;

import com.css.project.process.modeler.domain.HistoricActivity;
import com.css.project.process.modeler.service.IProcessService;
import com.css.project.system.domain.SysUser;
import com.css.project.system.mapper.SysUserMapper;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.task.Comment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class ProcessServiceImpl implements IProcessService {

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private RepositoryService repositoryService;


    @Autowired
    private SysUserMapper userMapper;

    @Override
    public List<HistoricActivity> selectHistoryList(String processInstanceId, HistoricActivity historicActivity,String auditOptionsKey) {
        List<HistoricActivity> activityList = new ArrayList<>();
        HistoricActivityInstanceQuery query = historyService.createHistoricActivityInstanceQuery();
        if (StringUtils.isNotBlank(historicActivity.getAssignee())) {
            query.taskAssignee(historicActivity.getAssignee());
        }
        if (StringUtils.isNotBlank(historicActivity.getActivityName())) {
            query.activityName(historicActivity.getActivityName());
        }
        List<HistoricActivityInstance> list = new ArrayList<>() ;
        //HistoricActivityInstance h1= query.processInstanceId(processInstanceId)
        //        .activityType("startEvent")
        //        .singleResult() ;
        //if(h1 != null ){
            //list.add(h1) ;
            List<HistoricActivityInstance> listTask = query.processInstanceId(processInstanceId)
                    .activityType("userTask")
                    .orderByHistoricActivityInstanceStartTime()
                    .asc()
                    .list();
            list.addAll(listTask);
        //}

        list.forEach(instance -> {
            HistoricActivity activity = new HistoricActivity();
            BeanUtils.copyProperties(instance, activity);
            String taskId = instance.getTaskId();
            List<Comment> comment = taskService.getTaskComments(taskId, "comment");
            if (!CollectionUtils.isEmpty(comment)) {
                activity.setComment(comment.get(0).getFullMessage());
            }
            if(StringUtils.isNotBlank(auditOptionsKey)){
                List<HistoricVariableInstance> listVariable = historyService.createHistoricVariableInstanceQuery().taskId(taskId).list() ;
                for(HistoricVariableInstance v :listVariable){
                    String[] keyList = auditOptionsKey.split(",") ;
                    boolean a = false ;
                    for(String v_key : keyList){
                        if(v.getVariableName().equals(v_key)){
                            activity.setAuditOptions(v.getValue());
                            a = true ;
                            break ;
                        }
                    }
                    if(a){
                        break ;
                    }
                }
            }
            if(StringUtils.isNotEmpty(instance.getAssignee())){
                SysUser sysUser = userMapper.selectUserById(Long.parseLong(instance.getAssignee()));
                if (sysUser != null) {
                    activity.setAssigneeName(sysUser.getNickName());
                }
            }
            activityList.add(activity);
        });
        return activityList;
    }

}
